package com.hmall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 苏成博
 * @date 2023-03-20 11:59
 */
@Configuration
public class MQConfig {
    //消息转换器
    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    //订单交换机声明
    @Bean
    public DirectExchange itemDirectExchange(){
        return new DirectExchange("order.direct.exchange");
    }
    //延迟队列声明
    @Bean
    public Queue ttlQueue(){
        return QueueBuilder.durable("ttl.order.queue")
                .deadLetterExchange("ttl.dead.exchange")
                .deadLetterRoutingKey("order.dead")
                .ttl(10000)
                .build();
    }
    //绑定商品交换机和延迟队列
    @Bean
    public Binding bindingItemExchangeTtlQueue(){
        return BindingBuilder.bind(ttlQueue()).to(itemDirectExchange()).with("order.ttl");
    }
    //死信交换机声明
    @Bean
    public DirectExchange deadDirectExchange(){
        return new DirectExchange("ttl.dead.exchange");
    }
    //处理死信的消息队列声明
    @Bean
    public Queue itemDeadQueue(){
        return new Queue("order.dead.queue");
    }
    //绑定商品交换机和延迟队列
    @Bean
    public Binding bindingDeadMessageQueue(){
        return BindingBuilder.bind(itemDeadQueue()).to(deadDirectExchange()).with("order.dead");
    }
}
